package com.crm.crm.interceptor;

import com.crm.crm.domain.ThreadEntity;
import com.crm.crm.util.JwtUtils;
import com.crm.crm.util.UserThreadLocalUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import java.util.Map;

@Slf4j
@Component
public class TokenInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //1. 获取请求url。
        String url = request.getRequestURL().toString();

        //2. 判断请求url中是否包含login，如果包含，说明是登录操作，放行。
        if(url.contains("login")){ //登录请求
            log.info("登录请求 , 直接放行");
            return true;
        }

        //3. 获取请求头中的令牌（token）。
        String jwt = request.getHeader("authorization");

        //4. 判断令牌是否存在，如果不存在，返回错误结果（未登录）。
        if(!StringUtils.hasLength(jwt)){ //jwt为空
            log.info("获取到jwt令牌为空, 返回错误结果");
            response.setStatus(HttpStatus.SC_UNAUTHORIZED);
            return false;
        }

        //5. 解析token，如果解析失败，返回错误结果（未登录）。
        try {
            String[] split = jwt.split(" ");
            jwt =  split[1];
            Map<String, Object> claims = JwtUtils.parseJWT(jwt);
            ThreadEntity threadEntity = new ThreadEntity();
            threadEntity.setId(Long.valueOf(claims.get("userId").toString()));
            threadEntity.setName(claims.get("username").toString());
            UserThreadLocalUtil.setCurrent(threadEntity);
            log.info("令牌合法, 放行");
        } catch (Exception e) {
            e.printStackTrace();
            log.info("解析令牌失败, 返回错误结果");
            response.setStatus(HttpStatus.SC_UNAUTHORIZED);
            return false;
        }

        //6. 放行。
        log.info("令牌合法, 放行");
      //  UserThreadLocalUtil.removeCurrentUser();
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        UserThreadLocalUtil.removeCurrentUser();
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }

}